<?php
/**
 * Created by PhpStorm.
 * User: taotao
 * Date: 2019/9/23
 * Time: 15:33
 */

namespace App\Http\Service;


use App\Models\CompanysModel;
use App\Models\JobsModel;
use App\Repositories\JobsRepository;

class ZhaoPinService
{

    protected $job_repository;
    public function __construct()
    {
        $this->job_repository = new JobsRepository();
    }
    /**zwp
     * 获取招聘总数和分页数
     * @param type $uid
     * @param type $pageSize
     * @return type
     */
    public function getAllCount($uid,$pageSize){
        $data = $this->job_repository->getAllCount($uid);
        if(!$data){
            return null;
        }
        $total = count($data->toArray());
        $page = ceil($total/$pageSize);
        return ["total"=>$total,"page"=>$page];
    }

    public function getZhaoPinLists($uid,$page,$pageSize){
        $result = [];
        $myJobs = $this->job_repository->getJobsLists($uid,$page,$pageSize);
        foreach ($myJobs as &$v){
            $job_id = $v["job"];
            $job = $this->job_repository->findJobById($job_id);
            $v["job"] = $job;
            $status = $v["status"];
            //状态：1工人投递中，2企业拒绝投递，3企业面试邀请，4工人拒绝邀请，5面试中，6面试失败，7面试成功，8工人评价，9企业评价，10完成
            switch ($status) {
                case 1:
                    $v["status"] = "工人投递中";

                    break;
                case 2:
                    $v["status"] = "企业拒绝投递";

                    break;
                case 3:
                    $v["status"] = "企业面试邀请";

                    break;
                case 4:
                    $v["status"] = "工人拒绝邀请";

                    break;
                case 5:
                    $v["status"] = "面试中";

                    break;
                case 6:
                    $v["status"] = "面试失败";

                    break;
                case 7:
                    $v["status"] = "面试成功";

                    break;
                case 8:
                    $v["status"] = "工人评价";

                    break;
                case 9:
                    $v["status"] = "企业评价";

                    break;
                case 10:
                    $v["status"] = "完成";

                    break;

                default:
                    $v["status"] = "未知";
                    break;
            }
            $v["created_at"] = \App\Jobs\Fun::date($v["created_at"]);
        }
        return $myJobs;
    }
    
    public function getZhaoPinDetail($id){
        $data = $this->job_repository->getZhaoPinDetail($id);
        if(!$data){
            return null;
        }
        $job_id = $data["job"];
        $job = $this->job_repository->findJobById($job_id);
        $data["job"] = $job;
        $education = $data["education"];
        //1小学；2初中；3高中；4大专；5本科；6研究生；7其他；
        switch ($education) {
            case 1:
                $data["education"] = "小学";

                break;
            case 2:
                $data["education"] = "初中";

                break;
            case 3:
                $data["education"] = "高中";

                break;
            case 4:
                $data["education"] = "大专";

                break;
            case 5:
                $data["education"] = "本科";

                break;
            case 6:
                $data["education"] = "研究生";

                break;
            case 7:
                $data["education"] = "其他";

                break;

            default:
                $data["education"] = "暂无";
                break;
        }
        //工作年限：1一年以内；2一至二年；3三至五年；4五年以上；
        $work_year = $data["work_year"];
        switch ($work_year) {
            case 1:
                $data["work_year"] = "一年以内";

                break;
            case 2:
                $data["work_year"] = "一至二年";

                break;
            case 3:
                $data["work_year"] = "三至五年";

                break;
            case 4:
                $data["work_year"] = "五年以上";

                break;

            default:
                $data["work_year"] = "暂无";
                break;
        }
        //福利待遇：0暂无；1包住；2包吃；
        $welfare = $data["welfare"];
        switch ($welfare) {
            case 0:
                $data["welfare"] = "暂无";

                break;
            case 1:
                $data["welfare"] = "包住";

                break;
            case 2:
                $data["welfare"] = "包吃";

                break;

            default:
                $data["welfare"] = "暂无";
                break;
        }
        return $data;
    }
    public function publishZhaoPin($uid,$data){
        $cid = $this->job_repository->findCidByUid($uid);
        if(!$cid){
            return false;
        }
        $data["cid"] = $cid;
        $time = time();
        $data["created_at"] = $time;
        $data["updated_at"] = $time;
        try{
            DB::beginTransaction();
            $origin_id = $this->job_repository->insertJob($data);
            if(!$origin_id){
                DB::rollback();
                return false;
            }
            $data = ["uid"=>$uid,"cid"=>$cid,"origin_id"=>$origin_id,"created_at"=>$time,"updated_at"=>$time];
            $flag = $this->job_repository->insertJobStaff($data);
            if(!$flag){
                DB::rollback();
                return false;
            }
            DB::commit();
            return true;
            
        }catch(\Exception $e){
            $this->msgerror=$e->getMessage();
            DB::rollback();
            \Log::error($e->getMessage());
            return false;
        }
        
        
    }

    /**
     * 获取用户的投递列表
     * @param $uid
     * @param $page
     * @param $pageSize
     * @return array
     */
    public function getUserJobs($uid,$page,$pageSize)
    {
        $result = [];
        $myJobs = $this->job_repository->getUserJobs($uid,$page,$pageSize);
        
        if($myJobs){
            foreach ($myJobs as $job){
                //查询公司名
                $company_name = CompanysModel::where('uid',$job['cid'])->value('com_name');
                $value['com_name'] = $company_name ?? '';
                $value['from'] = $job['from'];
                $value['status'] = $job['status'];
                $value['date'] = date('Y-m-d H:i:s',$job['created_at']);
                $result[] = $value;
            }
        }
        return $result;
    }

    /**
     * 用户投递详情
     * @param $id
     * @return array
     */
    public function getJobsDetail($id)
    {
        $result = [];
        $Jobs = $this->job_repository->getJobsDetail($id);
        if($Jobs){
            //查询公司名
            $company_name = CompanysModel::where('uid',$Jobs->cid)->value('com_name');
            $result['com_name'] = $company_name ?? '';
            //查询岗位名称
            $job_name = JobsModel::where('id',$Jobs->job)->value('name');
            $result['job_name'] = $job_name ?? '';
            $result['num'] = $Jobs->num;  //数量
            $result['emolument'] = $Jobs->emolument;  //薪酬
            $result['welfare'] = $Jobs->welfare;  //福利待遇：0暂无；1包住；2包吃；
            $result['rest'] = $Jobs->rest;  //休息制度：1单休；2双休；
            $result['education'] = $Jobs->education;  //学历：1小学；2初中；3高中；4大专；5本科；6研究生；7其他；
            $result['work_year'] = $Jobs->work_year;  //工作年限：1一年以内；2一至二年；3三至五年；4五年以上；
            $result['address'] = $Jobs->address;   //工作地点
            $result['detail'] = $Jobs->detail;  //岗位要求
            $result['content'] = $Jobs->content;   //工作内容
            $result['work_hour'] = $Jobs->work_hour;  //工作时间
            $result['contact_name'] = $Jobs->contact_name;  //联系人姓名
            $result['contact_telephone'] = $Jobs->contact_telephone;  //联系人号码
            $result['email'] = $Jobs->email;  //邮箱地址
            $result['sex'] = $Jobs->sex;  //性别：1男，2女
            $result['age_range'] = $Jobs->age_range;  //年龄区间
            $result['skill'] = $Jobs->skill;  //技术工种
            $result['job_status'] = $Jobs->job_status;   //1草稿，2投递中，3拒绝投递，4面试邀请，5拒绝邀请，6面试失败，7面试成功，8评价
        }
        return $result;
    }

}